/**
 * Created by jiangqifan on 16/3/1.
 */

require('../models/project.js');
var _            = require('lodash');
var mongoose  = require('mongoose');
var Project = mongoose.model('Project');

//var utility = require('utility');
//var uuid    = require('node-uuid');

/**
 * 根据项目名列表查项目列表
 * Callback:
 * - err, 数据库异常
 * - projects, 项目列表
 * @param {Array} names 项目名列表
 * @param {Function} callback 回调函数
 */
//exports.getProjectByNames = function (names, callback) {
//    if (names.length === 0) {
//        return callback(null, []);
//    }
//    Project.find({ name: { $in: names } }, callback);
//};

/**
 * 根据项目名查找项目
 * Callback:
 * - err, 数据库异常
 * - project, 项目
 * @param {String} name 项目名
 * @param {Function} callback 回调函数
 */
exports.getProjectsByName = function (name, callback) {
    Project.find({'name': name}, callback);
};

exports.getAllProjects = function(callback){
    Project.find(callback);
};
/**
 *
 */
 exports.addProject = function (projectInfo,callback) {
     var project = new Project(projectInfo);
     project.save(callback);
};

exports.getProjectById = function(project_id,callback){
    Project.findOne({ _id: project_id}, callback);
};

exports.pushProCert = function(project_id,cert_id,callback){
    this.getProjectById(project_id,function(err, project){
        project['certs'].push(cert_id);
        project.markModified('certs');
        project.save();
        callback(err,project)
    });
};

exports.removeProCert = function(project_id,cert_id,callback){
    this.getProjectById(project_id,function(err, project){
        project['certs'] = dropWhile(project['certs'], function(item){
            if(item == cert_id){
                return true;
            }
        });
        project.markModified('certs');
        project.save();
        callback(err,project)
    });
};

exports.addDependency = function(project_id,dependency_info,callback){
    this.getProjectById(project_id,function(err, project){
            project['dependencies'].push(dependency_info);
            project.markModified('dependencies');
            project.save();
            callback(err,project)
        }
    );
};


function dropWhile(array, predicate,item) {
    for(var i = 0; i < array.length; i++){
        if(predicate(array[i])){
            if(item){
                array.splice(i,1,item);
            }else{
                array.splice(i,1);
            }

        }
    }
    return array;
}

exports.removeDependency = function(project_id,dependency_info,callback){
    this.getProjectById(project_id,function(err, project){
        project['dependencies'] = dropWhile(project['dependencies'], function(item){
            if(item.name == dependency_info.name){
                return true;
            }
        });
        project.markModified('dependencies');
        project.save();
        callback(err,project)
      }
    );
};

exports.updateDependency = function(project_id,dependency_info,callback){
    this.getProjectById(project_id,function(err, project){
            project['dependencies'] = dropWhile(project['dependencies'], function(item){
                if(item.name == dependency_info.name){
                    return true;
                }
            },dependency_info);
            project.markModified('dependencies');
            project.save();
            callback(err,project)
        }
    );
};


exports.updateDependencies = function(project_id,dependencies,callback){
    this.getProjectById(project_id,function(err, project){
            project['dependencies'] = dependencies;
            project.markModified('dependencies');
            project.save();
            callback(err,project)
        }
    );
};

exports.pushConfig = function(project_id,cfg_id,callback){
    this.getProjectById(project_id,function(err, project){
        project['build_cfg'].push(cfg_id);
        project.markModified('build_cfg');
        project.save();
        callback(err,project)
    });
};


exports.removeConfig = function(project_id,cfg_id,callback){
    this.getProjectById(project_id,function(err, project){
        _.remove(project['build_cfg'],function(cfg){
            return cfg = cfg_id;
        });
        project.markModified('build_cfg');
        project.save();
        callback(err,project)
    });
};
exports.updateProject = function(project_id,update_info,callback){
    Project.update({_id:project_id},{$set:update_info},callback);

};
//
//exports.testQuery = function(callback){
//    Project.find({
//        "dependencies": {
//            "$elemMatch": { "name": "BHA"}
//        }
//    },callback)
//};